<?php
namespace Company\Controller;
use Think\Controller;
class StockController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
    }
	//生产计划单常用列表
	public function tslist(){
		$Book=M('Book', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$book_list=$Book->where(array('examined'=>2))->select();
		$section_list=$Section->select();
		$personnel_list=$Personnel->select();
		$company_list=$ContactsCompany->select();
		//当日流水号生成
		$map['_string']="make_add_time-UNIX_TIMESTAMP(DATE(NOW()))>0 and make_add_time-UNIX_TIMESTAMP(DATE(NOW()))<86400";
		$count=$m->where($map)->count();//当天添加的采购单数
		//echo $m->getlastsql();
		$count=$count+1;
		$serial_number="";
		if($count<10){
			$serial_number='0000'.strval($count);
		}
		if($count>=10 and $count<100){
			$serial_number='000'.$count;
		}
		if($count>=100 and $count<1000){
			$serial_number='00'.$count;
		}
		if($count>=1000 and $count<10000){
			$serial_number='0'.$count;
		}
		$this->assign('serial_number',$serial_number);//流水序号
		$this->assign('book_list',$book_list);
		$this->assign('section_list',$section_list);
		$this->assign('personnel_list',$personnel_list);
		$this->assign('company_list',$company_list);
	}
	//推送仓库
	public function checkstockapi(){
		$m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ids=I('ids');
		$id=$ids[0];
		$checkstock_info=$m->field('order_no,add_time,update_time,section_no,auditor,mark,people_make_no,people_purchasing_no,manua_id,all_check')->where(array('id'=>$id))->find();
		$checkstock_info['warehouse_code']=I('warehouse_code');
		$url=C('base_config.checkstock');
		$res=push($url,$checkstock_info);
		$res=json_decode($res,true);

		if($res['status']){
			$return['status']=1;
			$return['message']=$res['info'];
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['message']=$res['info'];
			$this->ajaxReturn($return);
		}
	}
	public function checkstockinfo(){
        $m=M('CheckstockInfo', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['goods_name'] = array('like','%'.$keywords.'%');
			}
			if($selval=='3'){
				$map['bar_code'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	
    public function index(){
		$g=session('agent');
        $user_id=$g['company_id'];
		$group_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$group_id);
        $where['id']=array('in',$group_id);
        $warehouse_list=M("warehouse")->field('id,warehouse_code,name')->where($where)->select();
		
        $m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		foreach($list as $k=>$v){
			$list[$k]['section_no']=$Section->where(array('no'=>$v['section_no']))->getField('name');
			$list[$k]['people_make_no']=$Personnel->where(array('no'=>$v['people_make_no']))->getField('name');
			$list[$k]['people_purchasing_no']=$Personnel->where(array('no'=>$v['people_purchasing_no']))->getField('name');
		}
		
		$this->assign("warehouse_list",$warehouse_list);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_configs(){
			$m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['add_time']=time();
			// echo $m->getlastsql();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_config(){
		$m=M('Checkstock',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$this->tslist();
		if(IS_POST){
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_config(){
		$this->tslist();
		$id=trim(I('id'));
		$m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_config(){
		$id=trim(I('id'));
		$m=M('Checkstock', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}